import { defineConfig, loadEnv } from "vite";
import react from "@vitejs/plugin-react-swc";
import Inspector from "vite-plugin-react-inspector";
import { resolve } from "path";
import AutoImport from "unplugin-auto-import/vite";

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const config = loadEnv(mode, "./");

  return {
    base: config.VITE_ENV === "development" ? "/" : "./",
    plugins: [
      react(),
      Inspector(),
      AutoImport({
        dirs: ["src/components/common/TableCell"],
        dts: "src/types/auto-import.d.ts",
      }),
    ],
    // 配置路径别名
    resolve: {
      alias: {
        "@": resolve(__dirname, "src"),
        "@store": resolve(__dirname, "src/store"),
        "@pages": resolve(__dirname, "src/pages"),
        "@assets": resolve(__dirname, "src/assets"),
        "@styles": resolve(__dirname, "src/styles"),
        "@components": resolve(__dirname, "src/components"),
        "@layout": resolve(__dirname, "src/components/Layout"),
        "@antd": resolve(__dirname, "src/components/Antd"),
        "@utils": resolve(__dirname, "src/utils"),
      },
    },
    // 配置跨域
    server: {
      port: 8888,
      host: "0.0.0.0",
      proxy: {
        "/api": {
          target: config.VITE_TARGET, // 会自动判断当前环境，对应上述三个文件中的target
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ""),
        },
      },
    },
    build: {
      outDir: "build", // 指定打包文件的输出目录
      emptyOutDir: true, // 打包时先清空上一次构建生成的目录
      chunkSizeWarningLimit: 2 * 1024,
      sourcemap: false,
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true,
        },
      },
      rollupOptions: {
        output: {
          chunkFileNames: "assets/js/[name]-[hash].js",
          entryFileNames: "assets/js/[name]-[hash].js",
          assetFileNames: "assets/[ext]/[name]-[hash].[ext]",
          manualChunks(id) {
            if (id.includes("node_modules")) {
              if (id.includes("react")) return "react";
              if (id.includes("react-dom")) return "react-dom";
              if (id.includes("react-router-dom")) return "react-router-dom";
              if (id.includes("antd")) return "antd";
              if (id.includes("@ant-design")) return "@ant-design";
              if (id.includes("axios")) return "axios";
              if (id.includes("dayjs")) return "dayjs";
              if (id.includes("echarts")) return "echarts";
            }
          },
        },
      },
    },
    css: {
      // css预处理器
      preprocessorOptions: {
        less: {
          // 支持内联 JavaScript
          javascriptEnabled: true, // 一般只需要配置  javascriptEnabled就行，modifyVars也可以稍微配置
          charset: false,
          /* modifyVars: {
            // 更改主题在这里
            "primary-color": "#52c41a",
            "link-color": "#1DA57A",
            "border-radius-base": "2px",
          }, */
          additionalData: '@import "./src/styles/global.less";',
        },
      },
    },
  };
});
